<template>
  <div>
    <template>
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
          <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 新增类别</el-breadcrumb-item>
        </el-breadcrumb>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="父级类别" prop="parentId">
            <el-input v-model="ruleForm.parentId"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="关键词列表" prop="keywords">
            <el-input v-model="ruleForm.keywords"></el-input>
          </el-form-item>
          <el-form-item label="图标图片" prop="icon">
            <el-input v-model="ruleForm.icon"></el-input>
          </el-form-item>
          <el-form-item label="排序序号" prop="sort">
            <el-input v-model="ruleForm.sort"></el-input>
          </el-form-item>
          <el-form-item label="是否启用">
            <el-switch
                v-model="ruleForm.enable"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#aaa">
            </el-switch>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
                v-model="ruleForm.isDisplay"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#aaa">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </div>
</template>

<script>
import BaseUrl from "@/http/BaseUrl";
import TokenAxios from "@/http/TokenAxios";

export default {
  data() {
    return {
      ruleForm: {
        parentId: '0',
        name: '测试的一级类别',
        keywords: '关键词1,关键词2,关键词3',
        icon: '无',
        sort: 199,
        enable: 0,
        isDisplay: 0
      },
      rules: {
        parentId: [
          {required: true, message: '请选择父级类别', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入类别名称', trigger: 'blur'},
          {
            pattern: /^(?:[\u4e00-\u9fa5]{1,5}|[\u4e00-\u9fa5a-zA-Z\s]{2,12})$/,
            message: '类别名称只能是1~5长度的汉字，或者2~12个长度的由英文、汉字、空格组成的名称！',
            trigger: 'blur'
          }
        ],
        keywords: [
          {required: true, message: '请输入关键词列表', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请上传类别的图标图片', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/, message: '排序序号必须是0~255之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = BaseUrl.MALL_ADMIN + '/categories/add-new';
          console.log('url = ' + url);

          TokenAxios.post(url, this.ruleForm).then((response) => {
            if (response.data.state == 20000) {
              alert('新增类别成功');
            } else {
              this.$message.error(response.data.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>